﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;

namespace ChineseSchool.UserControl.ThuNghiem
{
    public partial class LessonDetail : System.Web.UI.UserControl
    {
        private readonly B_Homework homeworkServices = new B_Homework();
        private readonly B_Lessons lessonService = new B_Lessons();
        private readonly B_Insert_Update_Delete crud = new B_Insert_Update_Delete();
        readonly B_User userService = new B_User();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //LoadUserInfo();

                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    if (Request.QueryString["id"].Length > 0)
                    {
                        BindData(Convert.ToInt32(Request.QueryString["id"]));
                    }
                }

                if (Request.ContentType.Contains("json") && !string.IsNullOrEmpty(Request.QueryString["id"]))
                {

                }
            }
        }


        private void BindData(int lessonId)
        {
            var objLesson = lessonService.GetByPK(lessonId);

            if (objLesson != null)
            {
                //lblLessonTitle.Text = objLesson.LessonTitle;
                lblLessonContent.Text = LoadLessonContent(lessonId);
            }

            var lstFile = homeworkServices.GetFilesOfLesson(lessonId);
            rptAttach.DataSource = lstFile;
            rptAttach.DataBind();
        }

        private string GetContentType(string fileExtension)
        {
            if (string.IsNullOrEmpty(fileExtension))
                return string.Empty;

            string contentType = string.Empty;
            switch (fileExtension)
            {
                case ".htm":
                case ".html":
                    contentType = "text/HTML";
                    break;

                case ".txt":
                    contentType = "text/plain";
                    break;

                case ".doc":
                case ".rtf":
                case ".docx":
                    contentType = "Application/msword";
                    break;

                case ".xls":
                case ".xlsx":
                    contentType = "Application/x-msexcel";
                    break;

                case ".jpg":
                case ".jpeg":
                    contentType = "image/jpeg";
                    break;

                case ".gif":
                    contentType = "image/GIF";
                    break;

                case ".pdf":
                    contentType = "application/pdf";
                    break;
            }

            return contentType;
        }

        protected void RptAttachItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var dowork = new M_Dowork();

            if (string.IsNullOrEmpty(Session["Username"].ToString()) && string.IsNullOrEmpty(Session["PassWord"].ToString()))
            {
                Response.Redirect("/Login.html#tologin");
            }
            else
            {

                string userName = Session["Username"].ToString();
                string userPassword = Session["PassWord"].ToString();

                dowork.LessonId = Convert.ToInt32(Request.QueryString["lessonid"]);
                dowork.UserId = userService.Selectby_username_password(userName, userPassword).UserID;

                crud.insert("Dowork", "DoworkId", dowork);

                var fileName = (LinkButton)e.Item.FindControl("lbtFileAttach");
                var splitStr = fileName.Text.Split('.');
                var getExtension = splitStr[splitStr.Length - 1];

                Response.ContentType = GetContentType("." + getExtension);
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName.Text);
                Response.TransmitFile(Server.MapPath("/FilesUpload/" + fileName.Text));
                Response.End();
            }
        }

        public string LoadLessonContent(int Id)
        {
            var str = "";

            var lesson = (new B_Lessons()).GetByPK(Id);
            if (lesson != null)
            {
                str += "<h1><a href=\"#\">" + lesson.LessonTitle + "</a></h1>";
                str += "<div class=\"article-like-comment-date\">";
                str +=
                    "    Posted by <a href=\"#author\">Author</a> on <time datetime=\"" + lesson.PostDate + "\">" + lesson.PostDate + "</time> in <a href=\"#category\">Category</a>";
                str += "    <a class=\"like\" href=\"#\"><i class=\"fa fa-thumbs-o-up\"></i>08</a>";
                str += "    <a class=\"comments\" href=\"#\"><i class=\"fa fa-comments-o\"></i>49</a>";
                str += "</div>";

                str += " <p>" + lesson.LessonContent + "</p>";

                str += "<div class=\"article-tags\">";
                str +=
                    "    Tag: <a href=\"#tag1\">tieng trung</a>, <a href=\"#tag2\">hoc tieng trung</a>, <a href=\"#tag3\">luyen thi hsk</a>";
                str += "</div>";

                Page.Title = "" + lesson.LessonTitle + "";
                Page.MetaDescription = "" + lesson.LessonTitle + "";
                Page.MetaKeywords = "" + lesson.LessonTitle + "";
            }
            else
            {
                Page.Title = "Học tiếng trung hiệu quả";
            }
            return str;
        }

        //protected void BtnPostCommentClick(object sender, EventArgs e)
        //{
        //    var userName = txtUserName.Text;
        //    var email = txtEmail.Text;
        //    var content = txtContent.Text;

        //    if (string.IsNullOrEmpty(userName))
        //    {
        //        Response.Write("<script type='text/javascript'>alert('Vui lòng điền họ tên');</script>");
        //        return;
        //    }

        //    if (string.IsNullOrEmpty(email))
        //    {
        //        Response.Write("<script type='text/javascript'>alert('Vui lòng điền email');</script>");
        //        return;
        //    }

        //    if (string.IsNullOrEmpty(content))
        //    {
        //        Response.Write("<script type='text/javascript'>alert('Vui lòng điền ý kiến');</script>");
        //        return;
        //    }

        //    var userComment = new M_UserComment();

        //    userComment.ModuleId = 1;
        //    userComment.ModuleName = "1";
        //    userComment.UserName = userName;
        //    userComment.Email = email;
        //    userComment.PostContent = content;
        //    userComment.PostDate = DateTime.Now;

        //    crud.insert("UserComment", "PostId", userComment);
        //}

        //private void LoadUserInfo()
        //{
        //    string userName = Session["Username"].ToString();
        //    string userPassword = Session["PassWord"].ToString();
        //    var currentUser = userService.Selectby_username_password(userName, userPassword);

        //    if (currentUser != null)
        //    {
        //        txtUserName.Text = currentUser.UserName;
        //        txtEmail.Text = currentUser.Email;
        //    }
        //}
    }
}